import numpy as np
import random
from collections import Counter
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings("ignore", category=Warning)


delta = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]]  # 几个搜索方向


class Solution:
    def find_list(self, row, col, arr):
        all_list = []
        cur_add_list = []
        for ii in range(row):
            for jj in range(col):
                if arr[ii][jj] != 0:
                    if [ii, jj] in cur_add_list:
                        continue
                    else:
                        cur_add_list.extend([ii, jj])
                        out_list = [[ii, jj]]
                        search_list = [[ii, jj]]
                        while search_list:
                            cur_search = []
                            for file in search_list:
                                for direction in delta:
                                    pos = [file[0] + direction[0], file[1] + direction[1]]
                                    if row - 1 < pos[0] or pos[0] < 0 or col -1 < pos[1] or pos[1] < 0:
                                        continue
                                    elif pos in out_list:
                                        continue
                                    else:
                                        if arr[pos[0]][pos[1]] != 0:
                                            cur_search.append(pos)
                                            out_list.append(pos)
                            search_list = cur_search
                        all_list.append(out_list)
                        cur_add_list.extend(out_list)

        SUM = []
        for file in all_list:
            sum_ = 0
            for num_ in file:
                sum_ += arr[num_[0]][num_[1]]
            SUM.append(sum_)
        return SUM

    def find_max_and_second_num(self,list):
        one = list[0]  # 最大
        second = 0  # 次大
        for i in range(1, len(list)):  # 从第二个元素开始对比
            if list[i] > one:
                second = one
                one = list[i]
            elif list[i] > second:
                second = list[i]
        return one, second

    def get_average(self,list):
        average_list = np.mean(list)
        return average_list


def main():
    n1 = 150
    n2 = 150
    num = np.zeros((n1, n2))
    s1 = np.array(num)
    s2 = np.trunc(s1)
    A = s2.astype(int)
    list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
    # p = 0.4
    list1 = [0] * 20
    list2 = [0] * 20
    list3 = [0] * 20
    list4 = [0] * 11
    list5 = []
    for p in range(1, 21, 1):
        if p<12:
            for i in range(0, n1):
                for j in range(0, n2):
                    a = random.choice(list)
                    if a <= p:
                        A[i][j] = 1
                    else:
                        A[i][j] = 0
            num_list = A.tolist()
            S = Solution()
            ADD = S.find_list(20, 20, num_list)
                #print(num_list)
                #print(ADD)
            max1,max2 = S.find_max_and_second_num(ADD)
            MEAN = S.get_average(ADD)
            #print(max1)
            list1[p-1] = max1
            list2[p-1] = max2
            list3[p-1] = MEAN
            list4[p-1] = ADD

#第三问
            res_1 = Counter(ADD)
            print(res_1)
            #print(ADD)
        #print(list1,list2,list3)
        #print(list4)

        if p>=12:
            for i in range(0, n1):
                for j in range(0, n2):
                    a = random.choice(list)
                    if a <= p:
                        A[i][j] = 1
                    else:
                        A[i][j] = 0
            num_list = A.tolist()
            S = Solution()
            ADD = S.find_list(20, 20, num_list)
            #print(num_list)
            #print(ADD)
            max1,max2 = S.find_max_and_second_num(ADD)
            MEAN = S.get_average(ADD)
            #print(max1)
            #print(ADD)

            ADD.remove(max1)
            list1[p-1] = max1
            list2[p-1] = max2
            list3[p-1] = MEAN

            list5.append(ADD)
#第三问
            res_2 = Counter(ADD)
            print(res_2)
            #print(ADD.remove(max1))
        #print(list1,list2,list3)
    #print(list1,list2)




#第一问
    plt.title('团簇最大值和次大值的变化')  # 折线图标题
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示汉字
    b=[0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95,1]
    plt.xlabel('p值')  # x轴标题
    plt.ylabel('数值')  # y轴标题
    plt.plot(b, list1, marker='.', markersize=3)  # 绘制折线图，添加数据点，设置点的大小
    plt.plot(b, list2, marker='.', markersize=3)

    plt.legend(['最大值', '次大值'])  # 设置折线名称

    plt.show()  # 显示折线图

#第二问
    plt.title('团簇的平均大小的变化')  # 折线图标题
    plt.rcParams['font.sans-serif'] = ['SimHei']  # 显示汉字
    b=[0.05,0.1,0.15,0.2,0.25,0.3,0.35,0.4,0.45,0.5,0.55,0.6,0.65,0.7,0.75,0.8,0.85,0.9,0.95,1]
    plt.xlabel('p值')  # x轴标题
    plt.ylabel('平均值')  # y轴标题
    plt.plot(b, list3, marker='.', markersize=3)  # 绘制折线图，添加数据点，设置点的大小

    plt.legend(['平均值'])  # 设置折线名称

    plt.show()  # 显示折线图


if __name__ == '__main__':
    main()